/*
 * GenerateDAO.java		1.00	2008. 01. 21
 *
 * Copyright (c) 2008 David.Kim(KJS). All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of David.Kim(KJS).  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with David.Kim(KJS).
 */
package doit.gen.util;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;

import doit.framework.util.Config;
import doit.gen.domain.Column;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : GenerateDAO.java
 * description : 테이블에 매핑 되는 DAO 생성
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008. 01. 21  leewan  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")
public class GenerateDAO implements BaseGenerate {
    
	public void generate( Map input, String tableName, List columns) throws Exception {
		String packageName = (String) input.get("packageName");
		String systemDiv = (String) input.get("systemDiv");
		String basePath = Config.getString("doit.generator.path.base");
        String parentPath = basePath + "/" + packageName;
		String childPath = Config.getString("doit.generator.path.dao");
		String tableComment = columns.size() > 0 ? ((Column)columns.get(0)).getTableComments() : "";
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( basePath, packageName);
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( parentPath, childPath);

		// 클래스명 분석
		String className = GeneratorUtil.getClassName( tableName);
		
		// 내용 생성
		StringBuffer sb = new StringBuffer();
		
		// 라이선스 생성
		sb.append( GeneratorUtil.printLicense(className + "DAO"));
		
		// 패키지 생성
		sb.append( GeneratorUtil.printPackage(packageName + "." + childPath));
		
		// 임포트 생성
		sb.append( printImport(packageName, className));
		
		// 자바독 생성
		sb.append( GeneratorUtil.printJavaDoc(className  + "DAO", systemDiv, tableComment, tableComment));
		
		// 클래스 시작
		sb.append( printClassStart(className));
		
		// 클래스
		sb.append( printClass(className, columns));
		
		// 클래스 끝
		sb.append( printClassEnd());
		
		// 파일 생성
		File file = GeneratorUtil.makeFile( parentPath + "/" + childPath + "/" + className + "DAO" + "." + "java");
		
		// 파일에 쓰기
		RandomAccessFile raf = new RandomAccessFile( file, "rw");
		GeneratorUtil.write( raf, sb.toString());
		
		// 파일 닫기
		raf.close();
	}

    public String printImport( String packageName, String className) {
        StringBuffer sb = new StringBuffer();
        
        sb.append("import java.util.List;").append("\n");
        sb.append("import java.util.Map;").append("\n");
        sb.append("\n");
        sb.append("import org.springframework.dao.DataAccessException;").append("\n");
        sb.append("\n");
        sb.append("import " + packageName + ".domain."+className+";").append("\n");
        sb.append("\n");
    
        return sb.toString();
    }
	
	public String printClass(String className, List columns) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("    Map get" + className +  "PageList( Map input) throws DataAccessException;").append("\n");
		sb.append("    List get" + className +  "List( Map input) throws DataAccessException;").append("\n");
		sb.append("    Map get" + className + "( Map input) throws DataAccessException;").append("\n");
		sb.append("    Object insert" + className +  "( " + className + " input) throws DataAccessException;").append("\n");
		sb.append("    int update" + className +  "( " + className + " input) throws DataAccessException;").append("\n");
		sb.append("    int updateField" + className +  "( " + className + " input) throws DataAccessException;").append("\n");
		sb.append("    int delete" + className +  "( " + className + " input) throws DataAccessException;").append("\n");
		sb.append("    int delete" + className +  "All( Map input) throws DataAccessException;").append("\n");
		sb.append("    Object exist" + className +  "( Map input) throws DataAccessException;").append("\n");
		return sb.toString();
	}

	public String printField(Column column) throws Exception {
		return null;
	}

	public String printClassStart( String className) {
		return "public interface " + className + "DAO" + " {\n";
	}
	
	public String printClassEnd() {
		return "}\n";
	}
}
